package com.kakarote.core.common.aspect;

import com.kakarote.core.common.TenantSkipHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 租户拦截跳过切面
 *
 * @author zjj
 * @date 2022/6/13
 */
@Aspect
@Component
public class TenantSkipAspect {

    @Pointcut("@annotation(com.kakarote.core.common.annotation.TenantSkip)")
    public void tenantSkipAspect() {
    }

    @Around("tenantSkipAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            TenantSkipHolder.setSkip(true);
            return joinPoint.proceed();
        } finally {
            TenantSkipHolder.remove();
        }
    }


    /**
     * 租户需要跳过的表
     */
    public interface TenantSkipTable {

        /**
         * 获取不需要租户过滤的表
         *
         * @return tables
         */
        public Collection<String> getSkipTables();
    }
}
